const http = require('http');
const env = require('jsdom').env;
const zlib = require('zlib');
const fs = require("fs");
const ping = require('ping');
const querystring = require('querystring');
//global.http_jquery = fs.readFileSync("./jquery.js", "utf-8");

process.on('uncaughtException', function (err) {
	next_submit();
});
global.http_get = function(opt, callback) {
	http.get(opt, function(res) {
		var chunks = [], encoding = res.headers['content-encoding'];
		if(encoding === 'undefined') res.setEncoding('utf-8');
		res.on('data', function (chunk){
			chunks.push(chunk);
		});
		res.on('end', function (){
			var buffer = Buffer.concat(chunks);
			if (encoding == 'gzip') {
				zlib.gunzip(buffer, function (err, decoded) {
					callback(decoded.toString(), res.headers);
				});
			} else if (encoding == 'deflate') {
				zlib.inflate(buffer, function (err, decoded) {
					callback(decoded.toString(), res.headers);
				});
			} else {
				callback(buffer.toString(), res.headers);
			}
		});
	});
};

global.http_post = function(opt, params, callback) {
	var data = "";
	if (params) data = querystring.stringify(params);
	if (!opt.headers) opt.headers = { };
	opt.headers['Content-Length'] = Buffer.byteLength(data);
	var req = http.request(opt, function(res) {
		var chunks = [], encoding = res.headers['content-encoding'];
		if(encoding === 'undefined') res.setEncoding('utf-8');
		res.on('data', function (chunk){
			chunks.push(chunk);
		});
		res.on('end', function (){
			var buffer = Buffer.concat(chunks);
			if (encoding == 'gzip') {
				zlib.gunzip(buffer, function (err, decoded) {callback(decoded.toString());});
			} else if (encoding == 'deflate') {
				zlib.inflate(buffer, function (err, decoded) {callback(decoded.toString());});
			} else {
				callback(buffer.toString());
			}
		});
	});
	req.write(data);
	req.end();
};

const qjga = require("./qjga");
global.successList = [];

function next_submit() {
	global.currentIndex = global.currentIndex + 1;
	if (global.currentIndex >= global.currentProxyList.length) return start();
	new qjga(global.currentProxyList[global.currentIndex], next_submit);
}
var runing = false;
function start() {
	if (runing) return;
	runing = true;
	get_proxys(function (ips) {
		runing = false;
		global.currentProxyList = ips;
		if (ips.length <= 0) {
			setTimeout(start, 1000 * 10);
			return;
		}
		console.log("提取代理成功 - 共:" + ips.length + "条");
		global.currentIndex = -1;
		next_submit();
	});
}
start();

function get_proxys(callback) {
	var list = [];
	try{
		global.http_get({ host: "svip.kuaidaili.com", port:80, path: `/api/getproxy/?orderid=968254557809252&num=100&b_pcchrome=1&b_pcie=1&b_pcff=1&protocol=1&method=2&an_an=1&an_ha=1&sp1=1&sp2=1&quality=2&sort=1&format=json&sep=1`, method: "GET",
			headers: {
				"Accept": 'application/json, text/javascript, */*; q=0.01',
				"Accept-Language":"zh-CN,zh;q=0.8", "Accept-Encoding": "gzip, deflate, sdch",
				"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.18 Safari/537.36"
			}
		}, function (html) {
			var json = JSON.parse(html);
			if (json.data.count > 0){
				json.data.proxy_list.forEach(function (proxy) {
					var ips = proxy.split(":");
					list.push({ip: ips[0], port: ips[1]});
				});
			}
			callback(list);
		});
	}catch(e){
		callback(list);
	}
}

